package states.mainmenu;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import resources.ResourceManager;
import states.States;
import states.gameplay.Enemy;
import states.gameplay.GameplayState;
import states.gameplay.Player;
import states.gameplay.Position;
import states.gameplay.shapors.EnemyCreepGenerator;
import states.gameplay.shapors.ShaporFactory;

import com.corvo.pulsor.Assets;

public class MainMenuGameState extends BasicGameState{

	private int _id;
	
	private Image _background;
	private ResourceManager _rm;
	
	public MainMenuGameState(int id, ResourceManager rm){
		_id = id;
		
		_rm = rm;
	}
	
	@Override
	public void init(GameContainer gc, StateBasedGame sbg)
			throws SlickException {
		_background = _rm.getImage(Assets.MAIN_MENU_BACKGROUND.getId());
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
			throws SlickException {
		_background.draw();
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		Input input = gc.getInput();
		// obtain the mouse input
		// get the mouse coords
		int mouseX = input.getAbsoluteMouseX();
		int mouseY = input.getAbsoluteMouseY();
		
		// check if action was performed
		if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
			if(mouseX > 0 && mouseX < 350 && mouseY > 400 && mouseY < 500){
				ShaporFactory factory = new ShaporFactory(_rm);
				
				// Base
				Image base =  _rm.getImage(Assets.ACTOR_BASE.getId());
				base.setCenterOfRotation(base.getWidth()/2, base.getHeight()/2);
				Player player = new Player(50, new Position(30, 5), factory.generateBase());
				
				List<EnemyCreepGenerator> generators = new ArrayList<EnemyCreepGenerator>();
				generators.add(factory.getEnemyCreepGenerator());
			
				List<Position> positions = new ArrayList<Position>();
				positions.add(new Position(2, 36));
				
				Enemy enemy = new Enemy(generators, positions);
				
				GameplayState dgs = (GameplayState) sbg.getState(States.MISSION_STATE_ID);
				dgs.initStateForGameplay(player, enemy);
				sbg.enterState(States.MISSION_STATE_ID, new FadeOutTransition(Color.black, 1000), new FadeInTransition(Color.black, 1000));
				
			}
			
			if(mouseX > 350 && mouseX < 720 && mouseY > 400 && mouseY < 500){
				ShaporFactory factory = new ShaporFactory(_rm);
				
				// Base
				Image base =  _rm.getImage(Assets.ACTOR_BASE.getId());
				base.setCenterOfRotation(base.getWidth()/2, base.getHeight()/2);
				Player player = new Player(30, new Position(49, 16), factory.generateBase());
				
				List<EnemyCreepGenerator> generators = new ArrayList<EnemyCreepGenerator>();
				generators.add(factory.getEnemyCreepGenerator());
				generators.add(factory.getEnemyCreepGenerator());
			
				List<Position> positions = new ArrayList<Position>();
				positions.add(new Position(1, 34));
				positions.add(new Position(1, 1));
				
				Enemy enemy = new Enemy(generators, positions);
				
				GameplayState dgs = (GameplayState) sbg.getState(States.MISSION_STATE_ID);
				dgs.initStateForGameplay(player, enemy);
				sbg.enterState(States.MISSION_STATE_ID, new FadeOutTransition(Color.black, 1000), new FadeInTransition(Color.black, 1000));
				
			}
			
			if(mouseX > 720 && mouseY > 400 && mouseY < 500){
				ShaporFactory factory = new ShaporFactory(_rm);
				
				// Base
				Image base =  _rm.getImage(Assets.ACTOR_BASE.getId());
				base.setCenterOfRotation(base.getWidth()/2, base.getHeight()/2);
				Player player = new Player(20, new Position(3, 3), factory.generateBase());
				
				List<EnemyCreepGenerator> generators = new ArrayList<EnemyCreepGenerator>();
				generators.add(factory.getEnemyCreepGenerator());
				generators.add(factory.getEnemyCreepGenerator());
				generators.add(factory.getEnemyCreepGenerator());
			
				List<Position> positions = new ArrayList<Position>();
				positions.add(new Position(8, 34));
				positions.add(new Position(30, 25));
				positions.add(new Position(45, 20));
				
				Enemy enemy = new Enemy(generators, positions);
				
				GameplayState dgs = (GameplayState) sbg.getState(States.MISSION_STATE_ID);
				dgs.initStateForGameplay(player, enemy);
				sbg.enterState(States.MISSION_STATE_ID, new FadeOutTransition(Color.black, 1000), new FadeInTransition(Color.black, 1000));
				
			}
		}
	}

	@Override
	public int getID() {
		return _id;
	}

}
